<?php

namespace App\Model;

use Hyperf\DbConnection\Model\Model;

/**
 * 资料审核日志
 */
class ProfileVerifyLog extends Model
{
    protected $table = 'user_profile_verify_log';

    const STATUS_ING = 0;
    const STATUS_OK = 1;
    const STATUS_BAD = -1;
    const STATUS_NO = -2;

    const TYPE_SELF_INTRO = 1;
    const TYPE_NICKNAME = 2;
    const TYPE_VOICE = 3;
    const TYPE_POSTER = 4;
    const TYPE_QA = 5;
    const TYPE_AVATAR = 6;
    const TYPE_VIDEO = 7;
    const TYPE_REAL_AVATAR = 8;
    const TYPE_WECHAT = 9;
    const TYPE = [
        self::TYPE_SELF_INTRO => '个性签名',
        self::TYPE_NICKNAME => '昵称',
        self::TYPE_VOICE => '语音签名',
        self::TYPE_POSTER => '照片墙',
        self::TYPE_QA => '专访回答',
        self::TYPE_AVATAR => '头像',
        self::TYPE_VIDEO => '视频',
        self::TYPE_REAL_AVATAR => '真人认证',
        self::TYPE_WECHAT => '微信认证',
    ];

    /**
     * 添加记录
     */
    static function addRecord(
        $userId = 0,
        $contentType = self::TYPE_NICKNAME,
        $content = '',
        $status = self::STATUS_OK
    ) {
        if (!$userId) return 0;

        //最近一条如果一样，则不记录
        if ($contentType != self::TYPE_POSTER) {
            $where = [
                ['user_id', $userId],
                ['content_type', $contentType],
            ];
//            $last = self::where($where)->orderBy('id', 'desc')->first();
//            if ($last && $last->content == $content) return 0;
            //修改之前同类记录状态
            self::where($where)->whereIn('status', [self::STATUS_OK, self::STATUS_ING])->update(['status' => self::STATUS_NO]);
        }

        //真人头像
        if ($contentType == self::TYPE_REAL_AVATAR && $status == self::STATUS_OK) {
            $where = [
                ['user_id', $userId],
                ['content_type', $contentType],
                ['status', self::STATUS_ING],
            ];
            $model = self::where($where)->orderBy('id', 'desc')->first();
            !$model && $model = new self();
        } else {
            $model = new self();
        }

        $profile = UserProfile::getProfile($userId);
//        if ($profile->gender == 1 && $status == self::STATUS_OK) $status = self::STATUS_ING;

        //添加记录
        $model->user_id = $userId;
        $model->content = is_string($content) ? $content : json_encode($content);
        $model->content_type = $contentType;
        $model->status = $status;
        return $model->saveOrFail() ? 1 : 0;
    }

    /**
     * 是否通过审核
     */
    static function checkVerified($userId = 0, $contentType = 0, $datatype = 'status')
    {
        if (!$userId || !$contentType) return 0;
        $where = [
            ['user_id', $userId],
            ['content_type', $contentType],
        ];
        $item = self::where($where)->orderBy('id', 'desc')->first();
        if ($datatype == 'status') return $item && $item->status == self::STATUS_OK ? 1 : 0;
        else return $item;
    }

    /**
     * 批量查询通过审核
     */
    static function batchCheckVerified($userId = 0, $contentTypes = [])
    {
        if (!$userId || empty($contentType)) return [];
        $where = [
            ['user_id', $userId],
        ];
        $item = self::where($where)->whereIn()->orderBy('id', 'desc')->first();
        return !$item || $item->status == self::STATUS_OK ? 1 : 0;
    }

    /**
     * 获取正在审核的照片
     */
    static function getVerfingPosters($userId = 0, $limit = 5)
    {
        $where = [
            ['user_id', $userId],
            ['content_type', self::TYPE_POSTER],
            ['status', self::STATUS_ING]
        ];
        return self::where($where)->orderBy('id', 'desc')->limit($limit)->pluck('content')->toArray();
    }

    /**
     * 今天检测次数
     */
    static function checkTodayCount($userId = 0, $contentType = 0)
    {
        if (!$userId || !$contentType) return 1000;
        $where = [
            ['user_id', $userId],
            ['content_type', $contentType],
            ['created_at', '>', date('Y-m-d')],
            ['status', '<>', self::STATUS_NO]
        ];
        return self::where($where)->count();
    }
}
